<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Binh
 * Date: 1/5/14
 * Time: 7:04 PM
 * To change this template use File | Settings | File Templates.
 */
use \Model;
use \Controller_Admin;

class Controller_Login extends Controller{
    //Khai bao trang cau truc login
    private $view;

    public function __construct(){
        $this->view = View::forge('login/layout');
        $this->view->head = View::forge('login/head');
        $this->view->header = View::forge('login/header');
        $this->view->form_login = View::forge('login/form_login');
        $this->view->footer = View::forge('login/footer');

        $this->set_error(null);
    }

    private function set_error($string){
        $this->view->set_global('error', $string);
    }

    public function action_layout_admin(){
        return $this->view;
    }

    public function action_error(){
        if(isset($_POST['user_name']) and $_POST['user_name'] != ""){
            if(isset($_POST['password']) and $_POST['password'] != ""){
                $data['USER_NAME'] = $_POST['user_name'];
                $data['PASSWORD'] = $_POST['password'];

                $result = \Model\Member::search_user($data);
                if( $result != null){
                    return (new Controller_Admin())->action_layout_home();
                }
                else{
                    $this->set_error("Nhập sai user hay password");
                    return $this->action_layout_admin();
                }
            }
            else{
                $this->set_error("Chưa nhập user, password");
                return $this->action_layout_admin();
            }
        }
        else{
            $this->set_error("Chưa nhập user, password");
            return $this->action_layout_admin();
        }
    }
}